package com.example.administrator.androidapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Ch4Activity3 extends AppCompatActivity implements View.OnFocusChangeListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ch4_3);

        EditText editText=(EditText)findViewById(R.id.ch4_3_email);
        editText.setOnFocusChangeListener(this);
        final TextView textView=(TextView)findViewById(R.id.ch4_3_tv);

        LinearLayout linearLayout=(LinearLayout) findViewById(R.id.ch4_3_ll);
        linearLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                float x=motionEvent.getX();
                float y=motionEvent.getY();

                textView.setText("x:"+x+",y:"+y);
                return true;
            }
        });
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        //参数b代表【获得/失去】焦点
        String EMAIL_REGEX = "^\\s*?(.+)@(.+?)\\s*$";
        Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);
        if(!b){
            //失去焦点
            EditText editText=(EditText)findViewById(R.id.ch4_3_email);
            TextView textView=(TextView)findViewById(R.id.ch4_3_tv);
            String content=editText.getText().toString();

            if(content.length()>0){
                Matcher matcher=EMAIL_PATTERN.matcher(content);
                if(matcher.matches()){
                    textView.setText("");

                }else {
                    textView.setText("invalid email");
                }

            }else {
                textView.setText("");
            }
        }

    }
}
